﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace V32Assembler
{
    public abstract class Output
    {
        private string Extension { get; set; }

        protected Output(string extension)
        {
            Extension = extension;
        }

        protected string OutFile(string inFile)
        {
            return (inFile.Contains('.') ? inFile.Remove(inFile.LastIndexOf('.')) : "") + "." + Extension;
        }

        public abstract string GetSuccessMessage(string inFile);
        public abstract string GetErrorMessage(string inFile);
        public abstract void Write(string inFile);
    }

    public class ObjectFileOutput : Output
    {
        private byte[] Bytes { get; set; }

        public ObjectFileOutput(byte[] bytes)
            : base("o")
        {
            Bytes = bytes;
        }

        public override string GetSuccessMessage(string inFile)
        {
            return string.Format("Successfully assembled '{0}': output is '{1}'.", inFile, OutFile(inFile));
        }
        public override string GetErrorMessage(string inFile)
        {
            return string.Format("Could not write file to '{0}'.", OutFile(inFile));
        }
        public override void Write(string inFile)
        {
            System.IO.File.WriteAllBytes(OutFile(inFile), Bytes);
        }
    }

    public class PreprocessedFileOutput : Output
    {
        private string[] Lines { get; set; }

        public PreprocessedFileOutput(string[] lines)
            : base("pasm")
        {
            Lines = lines;
        }

        public override string GetSuccessMessage(string inFile)
        {
            return string.Format("Successfully preprocessed '{0}': output is '{1}'.", inFile, OutFile(inFile));
        }
        public override string GetErrorMessage(string inFile)
        {
            return string.Format("Could not write file to '{0}'.", OutFile(inFile));
        }
        public override void Write(string inFile)
        {
            System.IO.File.WriteAllLines(OutFile(inFile), Lines);
        }
    }
    public class CommandAddressesFileOutput : Output
    {
        private string[] Lines { get; set; }

        public CommandAddressesFileOutput(string[] lines)
            : base("asmaddr")
        {
            Lines = lines;
        }

        public override string GetSuccessMessage(string inFile)
        {
            return string.Format("Successfully found command addresses in '{0}': output is '{1}'.", inFile, OutFile(inFile));
        }
        public override string GetErrorMessage(string inFile)
        {
            return string.Format("Could not write file to '{0}'.", OutFile(inFile));
        }
        public override void Write(string inFile)
        {
            System.IO.File.WriteAllLines(OutFile(inFile), Lines);
        }
    }
}
